package cn.fay.chapter15;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

/**
 * 构建一个洗牌的机器，它可以存入一副扑克牌，然后取出来的时候是已经洗过的随机顺序
 * @author emellend
 *
 */

public class ShuffleMachine<T> {
	private ArrayList<T> storage=new ArrayList<T>();
	private Random random=new Random();
	ShuffleMachine(){}
	ShuffleMachine(Collection<T> collection){
		storage.addAll(collection);
	}
	public void  add(T item) {
		storage.add(item);
	}
	private T next() {
		return storage.remove(random.nextInt(storage.size()));
	}
	
	public ArrayList<T> get() {
		ArrayList<T> result=new ArrayList<T>();
		while (!storage.isEmpty()) {
			result.add(next());
		}
		return result;
	}
	public static void main(String[] args) {
		ShuffleMachine<String> sm=new ShuffleMachine<String>();
		for (String s : "3 4 5 6 7 8 9 10 J Q K A 2".split(" ")) {
			sm.add(s);
		}
		System.out.println(sm.get());
	}
}
